package com.iotechn.unimall.admin.api.biaozhun;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.ShopCailiaoDO;
import com.iotechn.unimall.data.domain.ShopCailiaoPiciDO;
import com.iotechn.unimall.data.dto.goods.FileInfo;
import com.iotechn.unimall.data.mapper.ShopCailiaoMapper;
import com.iotechn.unimall.data.mapper.ShopCailiaoPiciMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class AdminCailiaoServiceImpl implements AdminCailiaoService {
    @Autowired
    private ShopCailiaoMapper shopCailiaoMapper;

 

    @Override
    public Page<ShopCailiaoDO> list(Long  shopId,Long  jiedianId,Long piciId,String title,Integer page, Integer limit,  Long adminId) throws ServiceException {
        Wrapper<ShopCailiaoDO> wrapper = new EntityWrapper<ShopCailiaoDO>();
        if(shopId!=null){
            wrapper.eq("shop_id",shopId);
        }
        if(jiedianId!=null){
            wrapper.eq("jiedian_id",jiedianId);
        }
        if(piciId!=null){
            wrapper.eq("pici_id",piciId);
        }
        if(!StringUtils.isEmpty(title)){
            wrapper.like("pici_name",title);
        }
        wrapper.orderBy("id", false);
        Integer count = shopCailiaoMapper.selectCount(wrapper);
        List<ShopCailiaoDO> ShopCailiaoDOS = shopCailiaoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
         ShopCailiaoDOS.forEach(item -> {
             Gson gson=new Gson();
             List<FileInfo> fileInfoList = gson.fromJson(item.getFile(), new TypeToken<List<FileInfo>>(){}.getType());
             item.setFileList(fileInfoList);
        });
        return new Page<>(ShopCailiaoDOS, page, limit, count);
    }
    @Override
    public List<ShopCailiaoDO> getBiaozhunByShopId(Long shopId,Long piciId) throws ServiceException {
         List<ShopCailiaoDO> ShopCailiaoDOS = shopCailiaoMapper.getBiaozhunByShopId(shopId,piciId);
         return ShopCailiaoDOS;
    }
    @Autowired
    private ShopCailiaoPiciMapper shopCailiaoPiciMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ShopCailiaoDO create(ShopCailiaoDO ShopCailiaoDO, Long adminId) throws ServiceException {
        ShopCailiaoDO.setAdminId(adminId);
        Date now = new Date();
        ShopCailiaoDO.setGmtCreate(now);
        ShopCailiaoDO.setGmtUpdate(now);
        if(ShopCailiaoDO.getFileList()!=null){
            Gson gson=new Gson();
            ShopCailiaoDO.setFile(gson.toJson(ShopCailiaoDO.getFileList()));
        }
        if(ShopCailiaoDO.getPiciId()!=null){
            ShopCailiaoPiciDO shopCailiaoPiciDO=shopCailiaoPiciMapper.selectById(ShopCailiaoDO.getPiciId());
            ShopCailiaoDO.setShopId(shopCailiaoPiciDO.getShopId());
            ShopCailiaoDO.setShopName(shopCailiaoPiciDO.getShopName());
        }
        if (shopCailiaoMapper.insert(ShopCailiaoDO) > 0) {
            ShopCailiaoDO.setId(ShopCailiaoDO.getId());
            return ShopCailiaoDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(ShopCailiaoDO ShopCailiaoDO, Long adminId) throws ServiceException {
        Long id = ShopCailiaoDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();
        if(ShopCailiaoDO.getFileList()!=null && ShopCailiaoDO.getFileList().size()>0){
            Gson gson=new Gson();
            ShopCailiaoDO.setFile(gson.toJson(ShopCailiaoDO.getFileList()));
        }
        if(ShopCailiaoDO.getPiciId()!=null){
            ShopCailiaoPiciDO shopCailiaoPiciDO=shopCailiaoPiciMapper.selectById(ShopCailiaoDO.getPiciId());
            ShopCailiaoDO.setShopId(shopCailiaoPiciDO.getShopId());
            ShopCailiaoDO.setShopName(shopCailiaoPiciDO.getShopName());
        }
        ShopCailiaoDO.setGmtUpdate(now);
        if (shopCailiaoMapper.updateById(ShopCailiaoDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (shopCailiaoMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids)) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        for (Long spuId : ids) {
            shopCailiaoMapper.deleteById(spuId);
        }
        return "ok";
    }


    @Override
    public ShopCailiaoDO detail(Long id, Long adminId) throws ServiceException {
        ShopCailiaoDO shopCailiaoDO= shopCailiaoMapper.selectById(id);
        if(shopCailiaoDO.getFile()!=null){
            Gson gson=new Gson();
            List<FileInfo> fileInfoList = gson.fromJson(shopCailiaoDO.getFile(), new TypeToken<List<FileInfo>>(){}.getType());
            shopCailiaoDO.setFileList(fileInfoList);
        }
        return shopCailiaoDO;
    }

    @Override
    public Boolean updateStatus(Long adminId, Long id, Integer status) throws ServiceException {
        if (status == null || (status != 0 && status != 1)) {
            throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
        }
        ShopCailiaoDO ShopCailiaoDO = new ShopCailiaoDO();
        ShopCailiaoDO.setId(id);
        ShopCailiaoDO.setGmtUpdate(new Date());
        if (shopCailiaoMapper.updateById(ShopCailiaoDO) > 0) {
            return true;
        }
        return false;
    }

}